package com.etc.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.etc.entity.Medicine;
import com.etc.service.MedicineService;

@Controller
public class MedicineController {
	@Resource
	private MedicineService medicineService;
	
	/**
	 * 查询所有药品
	 * @return
	 */
	@RequestMapping(value="medicine",method=RequestMethod.GET)
	public String selectMedicine(Model model,@RequestParam(value="page",required=true,defaultValue="0")int page,@RequestParam(value="pageSize",required=true,defaultValue="10")int pageSize) {		
		List<Medicine> medicine = 	medicineService.selectMedicineByPage(page, pageSize);
		int count=medicineService.selCountMedicine();
		model.addAttribute("medicine",medicine);
		model.addAttribute("count",count);
		model.addAttribute("page",page+1);
		return "forward:/back/drugs-management.jsp";
	}
	
	@RequestMapping(value="medicineById")
	public String selectMedicineById( int medicineId,Model model) {
		Medicine medicine = medicineService.selectMedicineById(medicineId);
		model.addAttribute("medicine", medicine);
		return "forward:/back/drugsManagement-edit.jsp";
	}
	
	/**
	 * 添加药品
	 * @param medicine
	 * @return
	 */
	@RequestMapping(value="addMedicine",method=RequestMethod.POST)
	@ResponseBody
	public boolean addMedicine(@RequestBody Medicine medicine) {
		boolean flag = medicineService.insertMedicine(medicine);
		System.out.println(medicineService.insertMedicine(medicine));
		return flag;
	}
	
	/**
	 * 修改
	 */
	@RequestMapping(value="updMedicine/{medicineid}",method=RequestMethod.PUT)
	@ResponseBody
	public boolean updMedicine(@PathVariable(value="medicineid") int medicineTd,@RequestBody Medicine medicine) {
		boolean flag = medicineService.updMedicine(medicine);
		return flag;
	}
	
	
	/**
	 * 删除
	 */
	@RequestMapping(value="delMedicine/{medicineid}",method=RequestMethod.DELETE)
	@ResponseBody
	public String delMedicine(@PathVariable(value="medicineid") int medicineid) {
		medicineService.delMedicine(medicineid);
		return "success";
	}
	
	/**
	 * 所有药品数量
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="countMedicine",method=RequestMethod.GET)
	@ResponseBody
	public int selCountMedicine() {
		int count = medicineService.selCountMedicine();
		return count;
	}
	/**
	 * 药品分页查询
	 * @return
	 *//*
	@RequestMapping(value="pageMedicine/{page}/{pageSize}",method=RequestMethod.GET)
	@ResponseBody
	public PageInfo<Medicine> selectByPage(@PathVariable int page,@PathVariable int pageSize){
		PageInfo<Medicine> medicine = medicineService.getMedicineByPage(page, pageSize);
		return medicine;
	}*/
	/**
	 * 查询所有类别
	 * @return
	 */
	@RequestMapping(value="selMedicineType",method=RequestMethod.POST)
	@ResponseBody
	public List<Medicine> selMedicineType() {
		return medicineService.selMedicineType();
	}
	
	@RequestMapping(value="selMedicineByType/{medicinetype}",method=RequestMethod.POST)
	@ResponseBody
	public List<Medicine> selMedicineByType(@PathVariable(value = "medicinetype") String medicinetype) {
		return medicineService.selMedicineByType(medicinetype);
	}
	
	/**
	 * 分页操作
	 * @param model
	 * @param page
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value="medicinePage/{page}/{pageSize}",method=RequestMethod.GET)
	@ResponseBody
	public List<Medicine> page(Model model,@PathVariable(value="page")int page,@PathVariable(value="pageSize")int pageSize) {		
		int startPage=(page-1)*pageSize;//从第几页开始显示数据
		List<Medicine> medicineList = medicineService.selectMedicineByPage(startPage, pageSize);
		int count=medicineService.selCountMedicine();
		model.addAttribute("medicine",medicineList);
		model.addAttribute("count",count);
		model.addAttribute("page",page);
		
		return medicineList;
	}
	
} 
